<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Time: 15:53
 */
namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Response;
class BaseController extends Controller
{
    public $enableCsrfValidation = false;

    public function renderJson($params = array())
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        return $params;
    }

    public function getParams($key)
    {
        $data = array_merge(yii::$app->request->post(),yii::$app->request->get());
        if (!empty($key)) {
            if (!empty($data[$key])) {
                return $data[$key];
            } else {
                return '';
            }
        } else {
            return $data;
        }
    }

    protected function renderJSONP($data=[], $msg ="ok", $code = 200)
    {
        header('Content-type: application/json');
        echo json_encode([
            "code" => $code,
            "msg"   =>  $msg,
            "data"  =>  $data,
            "req_id" =>  uniqid(),
        ]);
        return Yii::$app->end();
    }

    public function http_post($url, $data) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }
}